package com.slf.engine.bo;

import java.io.IOException;

import org.apache.log4j.Logger;
import org.apache.log4j.Priority;

import com.slf.engine.common.SysContants;
import com.slf.engine.common.SysContext;
import com.slf.engine.services.ITjfxConnService;
import com.slf.engine.services.helper.ServiceHelper;
import com.slf.engine.services.impl.BusServiceImpl;
import com.slf.engine.services.impl.TjfxConnServiceImpl;
import com.slf.engine.utils.SocketUtil;

public class WaitBean extends Thread {

	private static final Logger log = Logger.getLogger(WaitBean.class);
	
	private Order order;
	public Order getOrder() {
		return order;
	}
	public void setOrder(Order order) {
		this.order = order;
	}
	public long getTimeout() {
		return timeout;
	}
	public void setTimeout(long timeout) {
		this.timeout = timeout;
	}
	public Boolean getIsStart() {
		return isStart;
	}
	public void setIsStart(Boolean isStart) {
		this.isStart = isStart;
	}
	private long timeout;
	private Boolean isStart = true;
	private int queryNum = 0;
	public int getQueryNum() {
		return queryNum;
	}
	public void setQueryNum(int queryNum) {
		this.queryNum = queryNum;
	}
	public boolean removeFromList()
	{
		synchronized (this) {
			this.setIsStart(false);
			synchronized (SysContext.QUERY_MAP) {
				if(SysContext.QUERY_MAP.containsKey(order.getOrderId()))
				{
					SysContext.QUERY_MAP.remove(order.getOrderId());
					if(!SysContext.QUERY_MAP.containsKey(order.getOrderId()))
					{
						//当队列中不存在
						return true;
					}
				}
			}
		}
		return false;
	}
	
	public void run()
	{
		while(getIsStart()){
			if(getIsStart())
			{
				if(this.getTimeout() != 0 && System.currentTimeMillis() - this.getTimeout() >= 1000*20)//
				{
//					if(removeFromList())
//					{
						ITjfxConnService connService = new TjfxConnServiceImpl();
						this.setTimeout(System.currentTimeMillis());
						if(queryNum >= 3)
						{
							if(removeFromList())
							{
								order.setClbz(SysContants.CZ_UNKNOW);
								order.setCljg("在上游查询不到返回结果，人工核实");
								new BusServiceImpl().doBusResult(order);
							}
						}else{
							queryNum++;
							this.setQueryNum(queryNum);
							if(!connService.sendQueryMsg(order))
							{
								order.setClbz(SysContants.CZ_UNKNOW);
								order.setCljg("查询失败，上游网络断开");
								SysContext.IS_START = false;
								new BusServiceImpl().doBusResult(order);
							}
						}
						
//						log.log(Priority.INFO, "充值出现未知错误，发起主动查询");
//						String back = null;
//						try {
//							back = connService.sendQueryMsg(ServiceHelper.makeMsg(ServiceHelper.makeQueryBody(order).toString()).toString(),SocketUtil.getInStantClient());
//						} catch (IOException e) {
//							// TODO Auto-generated catch block
//							log.log(Priority.ERROR, "交易查询输出异常");
//							connService.closeServer();
//						}
//						if(back != null)
//						{
//							String type = back.substring(8, 12);
//							if("0311".equals(type))
//							{
//								
//							}else{
//								order.setClbz(SysContants.CZ_UNKNOW);
//								order.setCljg("充值异常结果，请平台人员校验，返回类型:" + type);
//							}
//						}else{
//							order.setClbz(SysContants.CZ_UNKNOW);
//							order.setCljg("充值异常结果，请平台人员校验");
//						}
//						new BusServiceImpl().doBusResult(order);
//					}
				}else{
					try {
						log.log(Priority.INFO, order.getOrderId()+"等待通知返回处理");
						Thread.sleep(1000*10);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
					}
				}
			}
		}
		
	}
}
